********************************************************************************
******************************* Robustness Checks ******************************
********************************** BZ 10km *************************************

clear

** we take reshaped data from DID_GFC_BZ20.do

use  "...\DID_GFC.dta"

****************************** prepare data ************************************

******** redefine treatment variables 

replace enacR_0912_B3=.
replace enacR_0912_B3=1  if dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade"
replace enacR_0912_B3=0 if !(dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade") & (yr_desigPa<2009 & dist_PADDD>10 & dist_PA==0)

******** drop unecessary observations

drop if enacR_0912_B3==.
drop if year<2000
drop if PADDD__==731
drop if PADDD__==723
drop if PADDD__==734

******** PA and PADDD indicators

gen PADDD_=1 if PADDD__!=.

sort ID1 year
bysort ID1: replace PADDD__=PADDD__[_n-1] if missing(PADDD__) 
replace PADDD_=1 if PADDD__!=.
replace PADDD_=0 if enacR_0912_B3==0

gen PA_=1 if PA!=.
bysort ID1: egen IDPA=max(PA)
bysort ID1: egen IDPADDD=max(PADDD)

gen ID_PA_PADDD=IDPADDD
replace ID_PA_PADDD=IDPA if ID_PA_PADDD==.

bysort ID1: egen PA0108=max(PA_) if year<2009
bysort ID1: egen PA0108_=max(PA0108) 
drop PA0108
rename PA0108_ PA0108

******** PADDD misallocation

replace ID_state=22 if (IDPADDD==696 | IDPADDD==695)

******** import PA designations

merge m:m PADDD using  "C:\Users\dkeles\Documents\these\article2\important\desigPA.dta", gen(mergedesig)
gen desig=1 if Designation=="Federal"
replace desig=1 if Designation=="federal"
replace desig=2 if Designation=="State"
replace desig=2 if Designation=="state"
bysort ID1: fillmissing desig, with(any)
drop mergedesig

gen federal=1 if desig==1
replace federal=0 if desig==2
gen state_=1 if desig==2
replace state_=0 if desig==1

******** redefine treatment variables 

replace enacR_0912_B3=.
replace enacR_0912_B3=1  if dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade"
replace enacR_0912_B3=0 if !(dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade") & (yr_desigPa<2009 & dist_PADDD>10 & dist_PA==0)

gen enacR_12_B3=1 if enacR_0912_B3==1 & year_PADDD==2012
replace enacR_12_B3=0 if enacR_0912_B3==0

gen R12protected2000_B3=1 if Rprotected2000_B3==1 & year_PADDD==2012
replace R12protected2000_B3=0 if Rprotected2000_B3==0 

gen enacR_10_B3=1 if enacR_0912_B3==1 & year_PADDD==2010
replace enacR_10_B3=0 if enacR_0912_B3==0

gen R10protected2000_B3=1 if Rprotected2000_B3==1 & year_PADDD==2010
replace R10protected2000_B3=0 if Rprotected2000_B3==0 

gen enacR_09_B3=1 if enacR_0912_B3==1 & year_PADDD==2009
replace enacR_09_B3=0 if enacR_0912_B3==0

gen R09protected2000_B3=1 if Rprotected2000_B3==1 & year_PADDD==2009
replace R09protected2000_B3=0 if Rprotected2000_B3==0 

sort ID1 year
replace enacR_12_B3=0 if enacR_0912_B3==1 & year_PADDD==2012 & year>=2000 & year<2012 
sort ID1 year
replace enacR_10_B3=0 if enacR_0912_B3==1 & year_PADDD==2010 & year>=2000 & year<2010 
sort ID1 year
replace enacR_09_B3=0 if enacR_0912_B3==1 & year_PADDD==2009 & year>=2000 & year<2009

gen enacR_10state_B3= enacR_10_B3*state_
replace enacR_10state_B3=0 if enacR_10_B3==0 & enacR_10state_B3==.

replace enacR_10state_B3=. if enacR_10state_B3==0 & enacR_10_B3==0 & state_==.
rename enacR_10state_B3 enacRS_10_B3

save  "...\DID_BZ10km_GFC.dta", replace

******** import data with matching results

use "...\results_impact_BZ10.dta"

keep ID1 *_weight 
drop if ID1==.

******** merge to reshaped panel data

merge 1:m ID1 using "...\DID_BZ10km_GFC.dta", nogen

save "...\DIDmatching_BZ10km_GFC.dta", replace

********************************* DID panel ************************************

clear all

use "...\DIDmatching_BZ10km_GFC.dta"

xtset ID1 year

****** Para (ATT)

** ID treated group
bysort ID1: egen enacR_12_B3_=max(enacR_12_B3)

** TWFE
areg treecoverloss enacR_12_B3 year agrifires tavg light _prec  if  PA0108==. & ID_state==14  & (enacR_12_B3_==1 | (enacR_12_B3==0 & para_weight!=.)) , absorb(ID1)
areg treecoverloss enacR_12_B3 year agrifires tavg light _prec if  PA0108==. & ID_state==14  & year>2007 & (enacR_12_B3_==1 | (enacR_12_B3==0 & para_weight!=.)) , absorb(ID1)

** DIDl table
did_multiplegt treecoverloss ID1 year enacR_12_B3  if  PA0108==. & ID_state==14  & (enacR_12_B3_==1 | (enacR_12_B3_==0 & rdo12PSM_pa_weight!=.)), ///
robust_dynamic dynamic(3) placebo(11) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995)

** DIDl figure and pretrest test
did_multiplegt treecoverloss ID1 year enacR_12_B3  if  PA0108==. & ID_state==14  & (enacR_12_B3_==1 | (enacR_12_B3_==0 & rdo12PSM_pa_weight!=.)), ///
robust_dynamic dynamic(3) placebo(3) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995) longdiff_placebo

****** rondonia (ATU)

** ID treated group

 bysort ID1: egen enacR_10_B3_=max(enacR_10_B3)

** TWFE
areg treecoverloss enacR_10_B3  year light agrifires tavg  _prec if PA0108==. & ID_state==22 & (enacR_10_B3_==1 & state_==1 | enacR_10_B3_==0 & state_==.) & (enacR_10_B3_==0 | enacR_10_B3_==1 & rondonia_weight!=.), absorb(ID1)
areg treecoverloss enacR_10_B3  year light agrifires tavg  _prec if  PA0108==. & ID_state==22 & (enacR_10_B3_==1 & state_==1 | enacR_10_B3_==0 & state_==.) & (enacR_10_B3_==0 | enacR_10_B3_==1 & rondonia_weight!=.) & year>2007, absorb(ID1)

** DIDl table
did_multiplegt treecoverloss ID1 year enacR_10_B3   if PA0108==. & ID_state==22 & (enacR_10_B3_==1 & state_==1 | enacR_10_B3_==0 & state_==.) & (enacR_10_B3_==0 | enacR_10_B3_==1 & rdo10PSM_rons_weight!=.), ///
robust_dynamic dynamic(5) placebo(9) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995)

** DIDl figure and pretrest test
did_multiplegt treecoverloss ID1 year enacR_10_B3  if PA0108==. & ID_state==22 & (enacR_10_B3_==1 & state_==1 | enacR_10_B3_==0 & state_==.) & (enacR_10_B3_==0 | enacR_10_B3_==1 & rondonia_weight!=.), ///
robust_dynamic dynamic(5) placebo(5) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995) longdiff_placebo

****** Roraima (ATU)

** ID treated group
bysort ID1: egen enacR_09_B3_=max(enacR_09_B3)

** TWFE
areg treecoverloss enacR_09_B3 year agrifires tavg _prec [fweight=rdo09PSM_ror_weight] if  PA0108==. & ID_state==23 &  (enacR_09_B3_==0 | enacR_09_B3_==1 & roraima_weight!=.) , absorb(ID1)
areg treecoverloss enacR_09_B3 year agrifires tavg _prec [fweight=rdo09PSM_ror_weight] if  PA0108==. & ID_state==23 & year>2007 & (enacR_09_B3_==1 | enacR_09_B3_==0 & roraima_weight!=.), absorb(ID1)

** DIDl table
did_multiplegt treecoverloss ID1 year enacR_09_B3  if  PA0108==. & ID_state==23  & (enacR_09_B3_==0 | enacR_09_B3_==1 & rdo09PSM_ror_weight!=.), ///
robust_dynamic dynamic(6) placebo(8) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995)

** DIDl figure and pretrest test
did_multiplegt treecoverloss ID1 year enacR_09_B3  if  PA0108==. & ID_state==23  & (enacR_09_B3_==0 | enacR_09_B3_==1 & roraima_weight!=.), ///
robust_dynamic dynamic(6) placebo(6) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995) longdiff_placebo


save "...\DIDmatching_BZ10km_GFC.dta", replace
